<?php
	header("Content-type:text/html;charset=utf-8");
	require_once 'AppConfig.php';
	require_once 'AppUtil.php';

	//统一支付接口
	function pay(){
		$params = array();
		$params["cusid"] = AppConfig::CUSID;
	  $params["appid"] = AppConfig::APPID;
	  $params["version"] = AppConfig::APIVERSION;
	  $params["orgid"] = '';
   	$params["trxamt"] = 1;
  	$params["reqsn"] = 123;
	  $params["paytype"] = 'W06';
	  $params["body"] = '主题';
  	$params["remark"] = 'ceshi';
	  $params["validtime"] = 12;
	  $params["acct"] = 'oM4tN4x6Cdp51AYTZCYzPN2E8gLU';
	  $params["notify_url"] = 'https://suppliershop.liancaiwang.cn/api/public/index.php/h5/pay/payApply';
	  $params["limit_pay"] ='';
	  $params["sub_appid"] = 'wx58959207ed2673ca';
  	$params["subbranch"] = '';
	  $params["cusip"] = '';
	  $params["idno"] = '';
	  $params["truename"] ='';
	  $params["fqnum"] ='';
	  $params["randomstr"] =12;
	  $params["signtype"] ='RSA';
		$params["front_url"] ='https://suppliershop.liancaiwang.cn/api/public/index.php/h5/pay/payApply';
    $params["sign"] = urlencode(AppUtil::Sign($params));//签名  
	  $paramsStr = AppUtil::ToUrlParams($params);
	    $url = AppConfig::APIURL . "/pay";
	    $rsp = request($url, $paramsStr);
	    echo "请求返回:".$rsp;
	    echo "<br/>";
	    $rspArray = json_decode($rsp, true); 
	    if(AppUtil::validSign($rspArray)){
	    	echo "验签正确,进行业务处理";
	    }
	    
	}
	
	
	//统一被扫接口		
	function scanqrpay(){
		$reqsn = 'TK202207071523462253';
		$randomstr = md5($reqsn);
		$authcode = '280564008450199163';
		$body = '';

		$params = array();
		$params["cusid"] = AppConfig::CUSID;
		$params["appid"] = AppConfig::APPID;
		$params["version"] = AppConfig::APIVERSION;
		// $params["orgid"] = $orgid;
		// $params["version"] = $version;
		$params["trxamt"] = '0.1';
		$params["reqsn"] = $reqsn;
		$params["body"] = $body;
		$params["remark"] = '1234567890987654321';
		$params["authcode"] = $authcode;
		$params["limit_pay"] = 'no_credit';
		// $params["subbranch"] =$subbranch;
		// $params["asinfo"] =$asinfo;
		// $params["idno"] = $idno;
		// $params["truename"] =$truename;
		$params["randomstr"] =$randomstr;
		// $params["fqnum"] =$fqnum;
		$params["signtype"] ='RSA';
		$params["sign"] = urlencode(AppUtil::Sign($params));//签名  
		$paramsStr = AppUtil::ToUrlParams($params);
		$url = AppConfig::APIURL . "/scanqrpay";
		$rsp = request($url, $paramsStr);
		echo "请求返回:".$rsp;
		echo "<br/>";
		// exit;
		$rspArray = json_decode($rsp, true); 
		if(AppUtil::validSign($rspArray)){
			echo "验签正确,进行业务处理";
			echo "验签正确,进行业务处理";
		}
	    
	}
	
	
	
	//当天交易用撤销
	function cancel(){
		  $params = array();
	  	$params["cusid"] = AppConfig::CUSID;
	    $params["appid"] = AppConfig::APPID;
	    $params["version"] = AppConfig::APIVERSION;
	    $params["trxamt"] = "1";
	    $params["reqsn"] = "123456788";
	     $params["signtype"] ='RSA';
	    $params["oldreqsn"] = "123456";//原来订单号
	    $params["randomstr"] = "1450432107647";//
   $params["sign"] = urlencode(AppUtil::Sign($params));//签名  
	    $paramsStr = AppUtil::ToUrlParams($params);
	    $url = AppConfig::APIURL . "/cancel";
	    $rsp = request($url, $paramsStr);
		echo "请求返回:".$rsp;
	    echo "<br/>";
	    $rspArray = json_decode($rsp, true); 
	      if(AppUtil::validSign($rspArray)){
	    	echo "验签正确,进行业务处理";
	    }
	}
	
	//当天交易请用撤销,非当天交易才用此退货接口
	function refund(){
		 $params = array();
		 $params["cusid"] = AppConfig::CUSID;
	    $params["appid"] = AppConfig::APPID;
	    $params["version"] = AppConfig::APIVERSION;
	    $params["trxamt"] = "1";
	    $params["reqsn"] = "1234567889";
	    $params["oldreqsn"] = "123456";//原来订单号
	    $params["randomstr"] = "1450432107647";//
	     $params["signtype"] ='RSA';
   $params["sign"] = urlencode(AppUtil::Sign($params));//签名  
	    $paramsStr = AppUtil::ToUrlParams($params);
	    $url = AppConfig::APIURL . "/refund";
	    $rsp = request($url, $paramsStr);
		echo "请求返回:".$rsp;
	    echo "<br/>";
	    $rspArray = json_decode($rsp, true); 
	    if(AppUtil::validSign($rspArray)){
	    	echo "验签正确,进行业务处理";
	    }
	}
	
	function query(){
		$params = array();
		$params["cusid"] = AppConfig::CUSID;
	    $params["appid"] = AppConfig::APPID;
	    $params["version"] = AppConfig::APIVERSION;
	    $params["reqsn"] = "123456";
	    $params["randomstr"] = "1450432107647";//
	     $params["signtype"] ='RSA';
	       $params["sign"] = urlencode(AppUtil::Sign($params));//签名 
	    $paramsStr = AppUtil::ToUrlParams($params);
	    $url = AppConfig::APIURL . "/query";
	    $rsp = request($url, $paramsStr);
		echo "请求返回:".$rsp;
	    echo "<br/>";
	    $rspArray = json_decode($rsp, true); 
	     if(AppUtil::validSign($rspArray)){
	    	echo "验签正确,进行业务处理";
	    }
	}
	
	//发送请求操作仅供参考,不为最佳实践
	function request($url,$params){
		$ch = curl_init();
		$this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
		curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		 
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		 
		$output = curl_exec($ch);
		curl_close($ch);
		return  $output;
	}
	
	//验签
	function validSign($array){
		if("SUCCESS"==$array["retcode"]){
			$signRsp = strtolower($array["sign"]);
			$array["sign"] = "";
			$sign =  strtolower(AppUtil::Sign($array));
			if($sign==$signRsp){
				return TRUE;
			}
			else {
				echo "验签失败:".$signRsp."--".$sign;
			}
		}
		else{
			echo $array["retmsg"];
		}
		
		return FALSE;
	}
	
	//  pay();
	scanqrpay();
	//cancel();
	//refund();
	//query();
?>